<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$billBookId  = 0;
$partyId     = 0;
$isEdit      = 0;
$billBookNo  = 1;
$msg         = isset($_GET['msg']) ? $$_GET['msg'] : "";
$queryField  = "";
$queryValue  = "";
$billDate    = "";
$party       = array();
$billBookArr = array();
$totalFine   = (isset($_POST['totFine']) && $_POST['totFine'] > 0) ? $_POST['totFine'] : 0;
$totalAmount = (isset($_POST['totAmount']) && $_POST['totAmount'] > 0) ? $_POST['totAmount'] : 0;
$removePrev  = (isset($_POST['removePrev'])) ? 1 : 0;

if(isset($_POST['billBookAddBtn']))
{
  if($_POST['isEdit'] != '1')
  {
//   echo $billBook = "SELECT billNo FROM vepaaribillmaster
//                   WHERE billNo = ".$_POST['billBookNo']."
//                    AND DATE_FORMAT(CURDATE(),'%Y-%m') = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."'";
//    $billBookRes = mysql_query($billBook);



    $billBookInsert = "INSERT INTO vepaaribillmaster (partyId, billBookDate, totalFine, totalAmount, showPrev)
                       VALUES (".$_POST['party'].",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                               ".$totalFine.",".$totalAmount.",".$removePrev.")";
    $billBookInsertRes = mysql_query($billBookInsert);
    $billBookId = mysql_insert_id();
  }
  else
  {
//    $billBook = "SELECT billNo FROM billbook
//                  WHERE billBookId != ".$_POST['billBookId']."
//                    AND billNo = ".$_POST['billBookNo']."
//                    AND DATE_FORMAT(CURDATE(),'%Y-%m') = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."'";
//    $billBookRes = mysql_query($billBook);

      $billBook = "SELECT billNo FROM vepaaribillmaster WHERE billNo = 94 AND DATE_FORMAT(CURDATE(),'%Y-%m') = (SELECT '".$_POST['curDateYear']."-".$_POST['curDateMonth']."' + INTERVAL -1 MONTH)";
      
  
      
    echo $updateBillBook = "UPDATE vepaaribillmaster SET ".$queryField." partyId = ".$_POST['party'].", billBookDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                                           totalFine = ".$totalFine.", totalAmount = ".$totalAmount."
                        WHERE billBookId = ".$_POST['billBookId'];
    $updateBillBookRes = mysql_query($updateBillBook);
    
    $deleteBillDetail = "DELETE FROM vepaaribillDetail
                          WHERE billBookId = ".$_POST['billBookId'];
    $deleteBillDetailRes = mysql_query($deleteBillDetail);
    $billBookId = $_POST['billBookId'];
  } 
  
  
  $loopCount = 0;
  while($loopCount < count($_POST['particulars']))
  {
    $netWeight = ($_POST['netWeight'][$loopCount] != '') ? $_POST['netWeight'][$loopCount] : 0;
    $touch     = ($_POST['touch'][$loopCount] != '') ? $_POST['touch'][$loopCount] : 0;
    $waste     = ($_POST['waste'][$loopCount] != '') ? $_POST['waste'][$loopCount] : 0;
    $fine      = ($_POST['fine'][$loopCount] != '') ? $_POST['fine'][$loopCount] : 0;
    $qty       = ($_POST['qty'][$loopCount] != '') ? $_POST['qty'][$loopCount] : 0;
    $labour    = ($_POST['labour'][$loopCount] != '') ? $_POST['labour'][$loopCount] : 0;
    $amount    = ($_POST['amount'][$loopCount] != '') ? $_POST['amount'][$loopCount] : 0;
    
    if($_POST['particulars'][$loopCount] != ''|| $_POST['netWeight'][$loopCount] != ''|| $_POST['touch'][$loopCount] != ''|| $_POST['waste'][$loopCount] != ''|| $_POST['fine'][$loopCount] != ''|| $_POST['qty'][$loopCount] != ''|| $_POST['labour'][$loopCount] != ''|| $_POST['amount'][$loopCount] != '')
    {
      $billBookDetail = "INSERT INTO vepaaribillDetail (billBookId, particulars, netWeight, touch, waste, fine, qty, labour, amount,CrDr)
                         VALUES (".$billBookId.",'".addslashes($_POST['particulars'][$loopCount])."',
                                 ".$netWeight.",".$touch.",".$waste.",".$fine.",
                                 ".$qty.",".$labour.",".$amount.",'Dr')";
      $billBookDetailRes = mysql_query($billBookDetail);
    }
    $loopCount++;
  }
 
}

if(isset($_GET['billBookId']) && $_GET['billBookId'] > 0)
{
  $isEdit = 1;
  
  $selectParty = "SELECT billBookId,billNo,partyId,billBookDate FROM vepaaribillmaster
                   WHERE billBookId = ".$_GET['billBookId'];
  $selectPartyRes = mysql_query($selectParty);
  $row = mysql_fetch_assoc($selectPartyRes);
  $partyId    = $row['partyId'];
  $billDate   = $row['billBookDate'];
  $billBookId = $row['billBookId'];
  $billBookNo = $row['billNo'];
  
  $selectBill = "SELECT billBookDetailId, billBookId, particulars, netWeight, touch, waste, fine, qty, labour, amount FROM vepaaribillDetail
                  WHERE billBookId = ".$_GET['billBookId'];
  $selectBillRes = mysql_query($selectBill);
  $a = 0;
  while($billRow = mysql_fetch_assoc($selectBillRes))
  {
    $billBookArr[$a]['id']          = $billRow['billBookDetailId'];
    $billBookArr[$a]['particulars'] = $billRow['particulars'];
    $billBookArr[$a]['netWeight']   = $billRow['netWeight'];
    $billBookArr[$a]['touch']       = $billRow['touch'];
    $billBookArr[$a]['waste']       = $billRow['waste'];
    $billBookArr[$a]['fine']        = $billRow['fine'];
    $billBookArr[$a]['qty']         = $billRow['qty'];
    $billBookArr[$a]['labour']      = $billRow['labour'];
    $billBookArr[$a]['amount']      = $billRow['amount'];
    $a++;
  }
}

// This for select party : Start
$selectParty = "SELECT partyId,partyName 
                  FROM party
                 ORDER BY partyName";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
$party['partyId'][$a]   = 0;
$party['partyName'][$a] = "Select";
$a++;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]   = $partyRow['partyId'];
  $party['partyName'][$a] = $partyRow['partyName'];
  $a++;
}
// This for select party : End
if($isEdit != 1)
{
  // This for select billBook No. : Start
  $selectBillBook = "SELECT (max(billNo)+1) AS billNo FROM vepaaribillmaster
                           WHERE MONTH(billBookDate) = ".date("m")."
                             AND YEAR(billBookDate)  = ".date("Y");
  $selectBillBookRes = mysql_query($selectBillBook);
  if($billBookRow = mysql_fetch_assoc($selectBillBookRes))
    $billBookNo = (int)$billBookRow['billNo'];
  // This for select billBook No. : End
}

$smarty = new smarty;
require("bottom.php");

$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_billBookDate",$l_billBookDate);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_netWeight",$l_netWeight);
$smarty->assign("l_touch",$l_touch);
$smarty->assign("l_waste",$l_waste);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_qty",$l_qty);
$smarty->assign("l_labour",$l_labour);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_add",$l_add);
$smarty->assign("l_remove",$l_remove);
$smarty->assign("l_addBillBook",$l_addBillBook);
$smarty->assign("l_billBook",$l_billBook);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_billBookDetailId",$l_billBookDetailId);
$smarty->assign("l_save",$l_save);

$smarty->assign("party",$party);
$smarty->assign("billBookNo",$billBookNo);
$smarty->assign("msg",$msg);
$smarty->assign("isEdit",$isEdit);
$smarty->assign("partyId",$partyId);
$smarty->assign("billDate",$billDate);
$smarty->assign("billBookArr",$billBookArr);
$smarty->assign("billBookId",$billBookId);
$smarty->display("vepaariBookNew.tpl");
?>